Micron Document
🎖️GitЯра🎖️


Displaying Raw • Download

core/repository/src/commonMain/kotlin/org/meshtastic/core/repository/ConnectionStateHolder.kt 4d4070c8e1ed73a56ee2f054300a0f62e3fe2e62 (4d4070c8) Text, 5.15 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.repository

Tff7b72import T7ee787kotlinx.atomicfu.locks.SynchronizedObject
Tff7b72import T7ee787kotlinx.atomicfu.locks.synchronized
Tff7b72import T7ee787kotlinx.coroutines.flow.MutableStateFlow
Tff7b72import T7ee787kotlinx.coroutines.flow.StateFlow
Tff7b72import T7ee787kotlinx.coroutines.flow.asStateFlow
Tff7b72import T7ee787org.meshtastic.core.model.ConnectionEpochs
Tff7b72import T7ee787org.meshtastic.core.model.ConnectionLifecycle
Tff7b72import T7ee787org.meshtastic.core.model.ConnectionState

T8b949e/** Owns canonical connection state, epochs, and compatibility projections under one publication lock. */
Tff7b72class T56d364ConnectionStateHolderTb4b4b4(
Te6edf3initialStateTb4b4b4: Te6edf3ConnectionState Tff7b72= Te6edf3ConnectionStateTb4b4b4.Te6edf3DisconnectedTb4b4b4,
Te6edf3initialEpochsTb4b4b4: Te6edf3ConnectionEpochs Tff7b72= Te6edf3ConnectionEpochsTb4b4b4(Tb4b4b4)Tb4b4b4,
Tb4b4b4) Tb4b4b4: Te6edf3ConnectionStateProvider Tb4b4b4{
Tff7b72init Tb4b4b4{
Te6edf3requireTb4b4b4(Te6edf3initialEpochsTb4b4b4.Te6edf3isSelfConsistentTb4b4b4(Tb4b4b4)Tb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ffinitialEpochs must satisfy connection epoch invariantsTa5d6ff" Tb4b4b4}
Tb4b4b4}

Tff7b72private Tff7b72val Te6edf3publicationLock Tff7b72= Te6edf3SynchronizedObjectTb4b4b4(Tb4b4b4)

Tff7b72private Tff7b72val Te6edf3mutableConnectionLifecycle Tff7b72=
Te6edf3MutableStateFlowTb4b4b4(Te6edf3ConnectionLifecycleTb4b4b4(Te6edf3version Tff7b72= T79c0ff0Tb4b4b4, Te6edf3state Tff7b72= Te6edf3initialStateTb4b4b4, Te6edf3epochs Tff7b72= Te6edf3initialEpochsTb4b4b4)Tb4b4b4)
Tff7b72private Tff7b72val Te6edf3mutableConnectionState Tff7b72= Te6edf3MutableStateFlowTb4b4b4(Te6edf3initialStateTb4b4b4)
Tff7b72private Tff7b72val Te6edf3mutableConnectionEpochs Tff7b72= Te6edf3MutableStateFlowTb4b4b4(Te6edf3initialEpochsTb4b4b4)

Tff7b72override Tff7b72val Te6edf3connectionLifecycleTb4b4b4: Te6edf3StateFlowTff7b72<Te6edf3ConnectionLifecycleTff7b72> Tff7b72= Te6edf3mutableConnectionLifecycleTb4b4b4.Te6edf3asStateFlowTb4b4b4(Tb4b4b4)
Tff7b72override Tff7b72val Te6edf3connectionStateTb4b4b4: Te6edf3StateFlowTff7b72<Te6edf3ConnectionStateTff7b72> Tff7b72= Te6edf3mutableConnectionStateTb4b4b4.Te6edf3asStateFlowTb4b4b4(Tb4b4b4)
Tff7b72override Tff7b72val Te6edf3connectionEpochsTb4b4b4: Te6edf3StateFlowTff7b72<Te6edf3ConnectionEpochsTff7b72> Tff7b72= Te6edf3mutableConnectionEpochsTb4b4b4.Te6edf3asStateFlowTb4b4b4(Tb4b4b4)

T8b949e/** Applies [newState] and advances epochs exactly once when the state changes. */
Tff7b72fun Td2a8ffsetConnectionStateTb4b4b4(Te6edf3newStateTb4b4b4: Te6edf3ConnectionStateTb4b4b4) Tff7b72= Te6edf3synchronizedTb4b4b4(Te6edf3publicationLockTb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3current Tff7b72= Te6edf3mutableConnectionLifecycleTb4b4b4.Te6edf3value
Tff7b72if Tb4b4b4(Te6edf3currentTb4b4b4.Te6edf3state Tff7b72!Tff7b72= Te6edf3newStateTb4b4b4) Tb4b4b4{
Te6edf3publishTb4b4b4(
Te6edf3ConnectionLifecycleTb4b4b4(
Te6edf3version Tff7b72= Te6edf3currentTb4b4b4.Te6edf3version Tff7b72+ T79c0ff1Tb4b4b4,
Te6edf3state Tff7b72= Te6edf3newStateTb4b4b4,
Te6edf3epochs Tff7b72= Te6edf3currentTb4b4b4.Te6edf3epochsTb4b4b4.Te6edf3advanceTb4b4b4(Te6edf3currentTb4b4b4.Te6edf3stateTb4b4b4, Te6edf3newStateTb4b4b4)Tb4b4b4,
Tb4b4b4)Tb4b4b4,
Tb4b4b4)
Tb4b4b4}
Tb4b4b4}

T8b949e/**
* Restores a known state, primarily for reusable test fakes. Epoch counters remain monotonic: omitting [epochs]
* applies the canonical transition into [state], while an explicit value may only advance the counters. The
* publication version always advances so readers can distinguish the reset from the preceding snapshot.
*/
Tff7b72fun Td2a8ffresetTb4b4b4(Te6edf3stateTb4b4b4: Te6edf3ConnectionState Tff7b72= Te6edf3ConnectionStateTb4b4b4.Te6edf3DisconnectedTb4b4b4, Te6edf3epochsTb4b4b4: Te6edf3ConnectionEpochs? Tff7b72= Tff7b72nullTb4b4b4) Tff7b72=
Te6edf3synchronizedTb4b4b4(Te6edf3publicationLockTb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3current Tff7b72= Te6edf3mutableConnectionLifecycleTb4b4b4.Te6edf3value
Tff7b72val Te6edf3minimumEpochs Tff7b72= Te6edf3currentTb4b4b4.Te6edf3epochsTb4b4b4.Te6edf3advanceTb4b4b4(Te6edf3currentTb4b4b4.Te6edf3stateTb4b4b4, Te6edf3stateTb4b4b4)
Tff7b72val Te6edf3nextEpochs Tff7b72= Te6edf3epochs Tff7b72?: Te6edf3minimumEpochs
Tff7b72val Te6edf3preservesCanonicalDeparture Tff7b72=
Te6edf3nextEpochsTb4b4b4.Te6edf3departures Tff7b72!Tff7b72= Te6edf3minimumEpochsTb4b4b4.Te6edf3departures Tff7b72|Tff7b72|
Te6edf3minimumEpochsTb4b4b4.Te6edf3lastDepartureState Tff7b72=Tff7b72= Tff7b72null Tff7b72|Tff7b72|
Te6edf3nextEpochsTb4b4b4.Te6edf3lastDepartureState Tff7b72=Tff7b72= Te6edf3minimumEpochsTb4b4b4.Te6edf3lastDepartureState
Te6edf3requireTb4b4b4(
Te6edf3nextEpochsTb4b4b4.Te6edf3departures Tff7b72>Tff7b72= Te6edf3minimumEpochsTb4b4b4.Te6edf3departures Tff7b72&Tff7b72&
Te6edf3nextEpochsTb4b4b4.Te6edf3completedHandshakes Tff7b72>Tff7b72= Te6edf3minimumEpochsTb4b4b4.Te6edf3completedHandshakes Tff7b72&Tff7b72&
Te6edf3nextEpochsTb4b4b4.Te6edf3handshakesAtLastDeparture Tff7b72>Tff7b72= Te6edf3minimumEpochsTb4b4b4.Te6edf3handshakesAtLastDeparture Tff7b72&Tff7b72&
Te6edf3nextEpochsTb4b4b4.Te6edf3isSelfConsistentTb4b4b4(Tb4b4b4) Tff7b72&Tff7b72&
Te6edf3preservesCanonicalDepartureTb4b4b4,
Tb4b4b4) Tb4b4b4{
Ta5d6ff"Ta5d6ffreset must not rewind epoch counters or contradict canonical departure evidenceTa5d6ff"
Tb4b4b4}
Te6edf3publishTb4b4b4(Te6edf3ConnectionLifecycleTb4b4b4(Te6edf3version Tff7b72= Te6edf3currentTb4b4b4.Te6edf3version Tff7b72+ T79c0ff1Tb4b4b4, Te6edf3state Tff7b72= Te6edf3stateTb4b4b4, Te6edf3epochs Tff7b72= Te6edf3nextEpochsTb4b4b4)Tb4b4b4)
Tb4b4b4}

T8b949e/** Publishes one authoritative snapshot and both legacy mirrors as one non-suspending critical section. */
Tff7b72private Tff7b72fun Td2a8ffpublishTb4b4b4(Te6edf3nextTb4b4b4: Te6edf3ConnectionLifecycleTb4b4b4) Tb4b4b4{
Te6edf3mutableConnectionLifecycleTb4b4b4.Te6edf3value Tff7b72= Te6edf3next
T8b949e// Epochs before state: state collectors that then read connectionEpochs must not observe lagging counters.
Te6edf3mutableConnectionEpochsTb4b4b4.Te6edf3value Tff7b72= Te6edf3nextTb4b4b4.Te6edf3epochs
Te6edf3mutableConnectionStateTb4b4b4.Te6edf3value Tff7b72= Te6edf3nextTb4b4b4.Te6edf3state
Tb4b4b4}
Tb4b4b4}

Tff7b72private Tff7b72fun Te6edf3ConnectionEpochsTb4b4b4.Td2a8ffisSelfConsistentTb4b4b4(Tb4b4b4)Tb4b4b4: Tffa657Boolean Tff7b72= Te6edf3departures Tff7b72>Tff7b72= T79c0ff0L Tff7b72&Tff7b72&
Te6edf3completedHandshakes Tff7b72>Tff7b72= T79c0ff0L Tff7b72&Tff7b72&
Te6edf3handshakesAtLastDeparture Tff7b72>Tff7b72= T79c0ff0L Tff7b72&Tff7b72&
Te6edf3handshakesAtLastDeparture Tff7b72<Tff7b72= Te6edf3completedHandshakes Tff7b72&Tff7b72&
Tb4b4b4(Te6edf3departures Tff7b72> T79c0ff0L Tff7b72|Tff7b72| Te6edf3handshakesAtLastDeparture Tff7b72=Tff7b72= T79c0ff0LTb4b4b4) Tff7b72&Tff7b72&
Te6edf3lastDepartureState Tff7b72!is Te6edf3ConnectionStateTb4b4b4.Te6edf3Connected Tff7b72&Tff7b72&
Tb4b4b4(Te6edf3departures Tff7b72=Tff7b72= T79c0ff0LTb4b4b4) Tff7b72=Tff7b72= Tb4b4b4(Te6edf3lastDepartureState Tff7b72=Tff7b72= Tff7b72nullTb4b4b4)

Served by rngit 1.5.0 - Generated in 0.07s